package com.yupi.yudada.score;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yupi.yudada.model.dto.question.QuestionContentDTO;
import com.yupi.yudada.model.entity.App;
import com.yupi.yudada.model.entity.Question;
import com.yupi.yudada.model.entity.ScoreResult;
import com.yupi.yudada.model.entity.UserAnswer;
import com.yupi.yudada.model.vo.QuestionVO;
import com.yupi.yudada.service.QuestionService;
import com.yupi.yudada.service.ScoreResultService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @Author ortonzhu
 * @Date 2024/11/16 17:09
 * 自定义得分
 */
@Component
public class CustomScoringScoreStrategy implements ScoreStrategy {
    @Resource
    private QuestionService questionService;

    @Resource
    private ScoreResultService scoreResultService;

    @Override
    public UserAnswer calculateScore(List<String> choice, App app) {
        // 1.根据id查询到题目和题目结果信息（按分数降序排序）
        Question question = questionService.getOne(
                Wrappers.lambdaQuery(Question.class)
                        .eq(Question::getAppId, app.getId()));

        List<ScoreResult> scoringResultList = scoreResultService.list(
                Wrappers.lambdaQuery(ScoreResult.class)
                        .eq(ScoreResult::getAppId, app.getId()).orderByDesc(ScoreResult::getResultScoreRange));

        // 统计用户的总得分
        QuestionVO questionVO = QuestionVO.objToVo(question);
        int totalScore = 0;
        List<QuestionContentDTO> questionContentDTO = questionVO.getQuestionContent();
        for (QuestionContentDTO contentDTO : questionContentDTO) {
            for (String answer : choice) {
                for (QuestionContentDTO.Option option : contentDTO.getOptions()) {
                    if (answer.equals(option.getKey())) {
                        String result = option.getResult();
                        int score = option.getScore();
                        totalScore += score;
                    }

                }
            }
        }
        // 遍历得分结果，找到第一个用户分数大于得分范围的结果，作为最终结果
        // 返回最大得分结果
        ScoreResult maxScoreResult = scoringResultList.get(0);
        for (ScoreResult result : scoringResultList) {
            if (totalScore >= result.getResultScoreRange()) {
                maxScoreResult = result;
                break;
            }
        }

        //4. 造返回值，填充答案对象的属性
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(app.getId());
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choice));
        userAnswer.setResultId(maxScoreResult.getId());
        userAnswer.setResultName(maxScoreResult.getResultName());
        userAnswer.setResultDesc(maxScoreResult.getResultDesc());
        userAnswer.setResultPicture(maxScoreResult.getResultPicture());
        userAnswer.setResultScore(totalScore);
        return userAnswer;
    }
}
